/*
 * @Author: szx
 * @Date: 2021-10-27 11:10:29
 * @LastEditTime: 2021-10-27 11:33:56
 * @Description:
 * @FilePath: \leetcode\977\977.cpp
 */

#include <iostream>
#include <vector>

using namespace std;

class Solution {
   public:
    vector<int> sortedSquares(vector<int>& nums) {
        int n = nums.size();
        int i = 0, j = n - 1;
        vector<int> v(n);
        for (int pos = n - 1; pos >= 0; pos--) {
            if (abs(nums[i]) > abs(nums[j])) {
                v[pos] = (nums[i] * nums[i]);
                i++;
            } else {
                v[pos] = (nums[j] * nums[j]);
                j--;
            }
        }
        return v;
    }
};

int main() {
    Solution s;
    vector<int> v = {-4, -1, 0, 3, 10};
    vector<int> res = s.sortedSquares(v);
    cout << "[";
    for (int& i : res) {
        cout << i << " ";
    }
    cout << "]" << endl;
    return 0;
}